import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login'
import Layout from '@/views/Layout'
import Articles from '@/views/articles'
import Image from '@/views/image'
import Comment from '@/views/comment'
import Fans from '@/views/fans'
import Settings from '@/views/settings'
import NotFound from '@/views/NotFound'
import AddArticle from '@/views/addArticle'
import EditArticle from '@/views/editArticle'
Vue.use(VueRouter)
// 配置路由
const routes = [
  {
    name: '/Layout',
    path: '/',
    component: Layout,
    children: [
      {
        name: 'Articles',
        path: '/articles',
        component: Articles
      },
      {
        path: '/editArticle/:id',
        name: 'editArticle',
        component: EditArticle
      },
      {
        name: 'Image',
        path: '/image',
        component: Image
      },
      {
        name: 'Comment',
        path: '/comment',
        component: Comment
      },
      {
        name: 'Fans',
        path: '/fans',
        component: Fans
      },
      {
        name: 'AddArticle',
        path: '/addArticle',
        component: AddArticle
      },
      {
        name: 'Settings',
        path: '/settings',
        component: Settings
      }
    ]
  },
  {
    name: 'Login',
    path: '/login',
    component: Login
  },
  {
    name: '*',
    path: '/NotFound',
    component: NotFound
  }
]
// 实例化路由
const router = new VueRouter({
  routes
})
// 路由守卫
router.beforeEach((to, form, next) => {
  console.log(to)
  console.log(form)
  if (to.path === '/') {
    let token = ''
    const obj = JSON.parse(localStorage.getItem('info'))
    if (obj) {
      token = obj.token
      console.log(token)
    }
    if (token) {
      next()
      console.log(1)
    } else {
      next('/login')
      console.log(2)
    }
  } else {
    next()
  }
})
// 暴漏路由
export default router
